package com.lcxw.hxzbadmin.controller.admincontroller;

import com.alibaba.fastjson.JSONObject;
import com.lcxw.hxzbadmin.entity.requestbean.RequestBodyBasic;
import com.lcxw.hxzbcommon.common.AesCBCUtil;
import com.lcxw.hxzbcommon.entity.ErrorCode;
import java.util.List;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lcxw.hxzbadmin.entity.pojo.AdminUser;
import com.lcxw.hxzbadmin.service.baseservice.AdminUserService;
import com.lcxw.hxzbapi.entity.response.Response;
import com.lcxw.hxzbcommon.common.RedisUtils;
import com.lcxw.hxzbcommon.common.StringUtil;

@RestController
@RequestMapping("/admin")
@CrossOrigin
public class AdminConfigController {

    private Logger logger = LoggerFactory.getLogger(AdminConfigController.class);

    @Resource
    AdminUserService adminUserService;

    @Resource
    RedisUtils redisUtils;

    @PostMapping("/password/reset")
    public Response resetAdminPassword(@RequestBody JSONObject requestParams) {
        String adminName = requestParams.getString("adminName");
        String oldPassword = requestParams.getString("oldPassword");
        AesCBCUtil instance = AesCBCUtil.getInstance();
        String newPassword = requestParams.getString("newPassword");
        try {
            oldPassword = instance.encrypt(oldPassword);
            newPassword = instance.encrypt(newPassword);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("加密失败");
        }

        logger.info("admin {} reset password", adminName);

        try {
            AdminUser adminUser = adminUserService.queryAdminByName(adminName);
            if (!oldPassword.equals(adminUser.getPassword())) {
                return Response
                    .common(logger, "原密码错误。", ErrorCode.INVALID_PARAM, "wrong old passwod.", null);
            }
            adminUser.setPassword(newPassword);
            adminUserService.updateAdminByName(adminUser);

            String redisKey = "admin:" + adminName + ":token";
            redisUtils.string_delete(redisKey);
        } catch (Exception e) {
            return Response.dbFail(logger, "operate admin " + adminName);
        }

        return Response.common(logger, adminName + "reset password success.", 0, "success", null);

    }

    @PostMapping("/add")
    public Response addAdmin(@RequestBody JSONObject requestParams) {
        logger.info("admin {} add a new admin user.", requestParams.getString("adminName"));

        AdminUser adminUser = new AdminUser();
        adminUser.setAdminName(requestParams.getString("newAdminName"));
        String password = requestParams.getString("password");
        try {
            password = AesCBCUtil.getInstance().encrypt(password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        adminUser.setPassword(password);
        adminUser.setAddTime(StringUtil.getDateTime());

        try {
            adminUserService.insert(adminUser);
        } catch (Exception e) {
            return Response.dbFail(logger, "add a new admin user.");
        }

        return Response.ok();

    }

    @PostMapping("/delete")
    public Response deleteAdmin(@RequestBody JSONObject requestParams) {
        String adminName = requestParams.getString("adminName");

        if (!"admin".equals(adminName)) {
            return Response.common(logger, "delete admin fail,the user is not super admin.",
                ErrorCode.LOSE_EFFICACY, "no access,not super admin.", null);
        }

        int id = requestParams.getInteger("id");

        if (id == 1) {
            return Response.error(ErrorCode.NOT_SUPPLY, "admin超级管理员不能删除");
        }

        try {
            adminUserService.deleteByPrimaryKey(id);
        } catch (Exception e) {
            return Response.dbFail(logger, "delete an admin user.");
        }

        return Response.ok();

    }

    @PostMapping("/getAll")
    public Response getAllAdminUser(@RequestBody RequestBodyBasic requestBody) {
        List<AdminUser> allAdminUser = adminUserService.getAllAdminUser();
        return Response.ok(allAdminUser);
    }

    @PostMapping("/mod/password")
    public Response modAdminPassword(@RequestBody JSONObject requestParams) {
        String adminName = requestParams.getString("adminName");
        if (!adminName.equals("admin")) {
            return Response.error(ErrorCode.NO_PERMISSION, "非超级管理员，无法重置其他管理员密码");
        }

        String modifyAdminName = requestParams.getString("modifyAdminName");
        AdminUser adminUser = adminUserService.queryAdminByName(modifyAdminName);
        String password = null;
        try {
            password = AesCBCUtil.getInstance().encrypt("123456");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("加密失败");
        }
        adminUser.setPassword(password);
        adminUserService.updateAdminByName(adminUser);
        return Response.ok();
    }

}
